from django.db import models

from apps.master.models import SerialNumberModel, DepartmentsModel
from bases.model import APIModel


class ProjectPlanHModel(APIModel):
    serialNumberFK = models.ForeignKey(SerialNumberModel, on_delete=models.DO_NOTHING)
    project_name = models.CharField(verbose_name='项目名称', max_length=50,blank=True, null=True)
    start_date = models.DateField(verbose_name='开始时间',null=False)
    end_date = models.DateField(verbose_name='结束时间',null=False)
    departmentFK = models.ForeignKey(DepartmentsModel, on_delete=models.DO_NOTHING)
    standard_curr = models.CharField(verbose_name='标准币种', max_length=3,blank=True, null=True)
    project_status = models.CharField(verbose_name='项目状态', max_length=1,default='O')

    class Meta:
        db_table = 'PROJECTPLANHF'
        verbose_name = '项目计划书主表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.project_name


class VersionHistoryModel(APIModel):
    projectPlanFK = models.ForeignKey(ProjectPlanHModel, related_name='versionhistories', on_delete=models.DO_NOTHING)
    version = models.IntegerField(verbose_name='版本号',default=0)
    version_date = models.DateField(verbose_name='版本记录时间', blank=True, null=True)
    note = models.CharField(verbose_name='改版内容', max_length=150,blank=True, null=True)

    class Meta:
        db_table = 'VERSIONHISTORY'
        verbose_name = '项目版本履历表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.version


class ProjectInfoModel(APIModel):
    projectPlanFK = models.ForeignKey(ProjectPlanHModel, on_delete=models.DO_NOTHING)
    project_type = models.CharField(verbose_name='项目类别', max_length=10, blank=True, null=True)
    scope = models.CharField(verbose_name='软件包范围版本', max_length=100, blank=True, null=True)
    obj_product = models.CharField(verbose_name='对象制品', max_length=100, blank=True, null=True)
    internal_target = models.CharField(verbose_name='内部项目目标', max_length=255, blank=True, null=True)
    customer_target = models.CharField(verbose_name='客户项目目标', max_length=255, blank=True, null=True)
    responsibility = models.CharField(verbose_name='职责范围', max_length=200, blank=True, null=True)
    partner_company = models.CharField(verbose_name='合作公司', max_length=100, blank=True, null=True)


    class Meta:
        db_table = 'PROJECTINFO'
        verbose_name = '项目基础信息表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.project_type


class ProjectCustomerModel(APIModel):
    projectPlanFK = models.ForeignKey(ProjectPlanHModel, on_delete=models.DO_NOTHING)
    customer_code = models.CharField(verbose_name='客户代码', max_length=12, blank=True, null=True)
    customer_name = models.CharField(verbose_name='客户名称', max_length=50, blank=True, null=True)
    customer_url = models.CharField(verbose_name='客户URL', max_length=50, blank=True, null=True)
    sales_amount = models.DecimalField(verbose_name='销售金额', decimal_places=6, max_digits=18, blank=True, null=True)
    customer_product = models.CharField(verbose_name='主要产品', max_length=50, blank=True, null=True)
    product_form = models.CharField(verbose_name='生产形态', max_length=50, blank=True, null=True)
    factory_num = models.IntegerField(verbose_name='据点工厂数')
    stock_clusters_num = models.IntegerField(verbose_name='据点在库集群数')
    address =  models.CharField(verbose_name='据点所在地', max_length=50, blank=True, null=True)
    product_num = models.IntegerField(verbose_name='产品数')
    item_num = models.IntegerField(verbose_name='品目总数')

    class Meta:
        db_table = 'PROJECTCUSTOMER'
        verbose_name = '客户信息表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.customer_code

class CommonSectionTypeModel(APIModel):
    # 辅助信息类型Model，类型+类别为唯一的
    section = models.CharField(verbose_name='类型', max_length=10, blank=True, null=True)
    section_item = models.CharField(verbose_name='类别', max_length=30, blank=True, null=True)

    class Meta:
        db_table = 'SYSCOMMONTYPE'
        verbose_name = '通用辅助信息表'
        verbose_name_plural = verbose_name

class CommonSectionModel(APIModel):
    projectPlanFK = models.ForeignKey(ProjectPlanHModel, on_delete=models.DO_NOTHING)
    sectionFK = models.ForeignKey(CommonSectionTypeModel, on_delete=models.DO_NOTHING)
    value = models.CharField(verbose_name='值', max_length=60, blank=True, null=True)

    class Meta:
        db_table = 'SYSCOMMONCODE'
        verbose_name = '通用辅助信息表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.projectPlanFK
